package com.axon.lemon
import com.sun.org.apache.xpath.internal.operations.And;
import grails.converters.JSON

class UserProfileController {
	
	
	
    def logout() {
        println("logout()")
		session.invalidate()
		redirect(controller:"index",action: "login")
    }
	
	def updateUser (){	
		def email=session.user.email;
		print(email);
		def user=UserProfile.findByEmail(email);
		[user:user]
	}
	def basicinfo(){
		def email=params.email
		print(email)
		def user=UserProfile.findByEmail(email);
	    def pass=params.pass;
	    def name=params.name;
	    def birth=params.birthday;
		print(birth)
		if(birth){
		def birthday= new Date().parse("MM/dd/yyyy", birth);
		print(birthday)
		user.birthday=birthday
		}
		def gender=params.gender;
//		print(email+pass+name+birthday+gender);
		
		user.password=pass
		user.fullName=name
		
		user.gender=gender
		user.save();
		def user1=UserProfile.findByEmail(email);
		///render(template: "userInfo", model:[userInfo: user1])
		render user1 as JSON
	}
	def companyinfo(){
		def email=params.email
		def company=params.company
		def website=params.website;
		def user=UserProfile.findByEmail(email);
		user.website=website
		user.company=company
		user.save();
		def user1=UserProfile.findByEmail(email);
		///render(template: "userInfo", model:[userInfo: user1])
		render user1 as JSON
	}
	def uploadavatar(){
		def email=params.email;
		print email
		def f = request.getFile('useravatar');
		byte[] img = f.getBytes();
		print img.size()
		if(img.size()>0){
		def user=UserProfile.findByEmail(email);
		print user
		user.avatar=img;
		user.save();
		}
		redirect(action: "updateUser");
	}

	def resetpw(){
		render "Reset password"
	}
	
	public static void main(String[] args) {
		UserProfile
	}
}
